import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import List from '../components/goods/List.vue'
import Add from '../components/goods/Add.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'

Vue.use(VueRouter)


const router = new VueRouter({
        routes: [
            { 
                path: '/', 
                meta:{
                    title: '登录'
                }, 
                redirect: '/login' 
            }, /* 重定向到login */
            { 
                path: '/login',
                meta:{
                    title: '登录'
                }, 
                component: Login 
            },
            { 
                path: '/welcome',
                meta:{
                    title: '首页'
                },
                component: Welcome
            },
            {
                path: '/home',
                component: Home,
                redirect: '/welcome',
                /* 重定向到welcome */

                children: [
                    { 
                        path: '/users',
                        name:'users',
                        meta:{
                            title: '用户管理'
                        },
                        component: Users
                     },
                    { 
                        path: '/Rights', 
                        name:'Rights',
                        meta:{
                            title: '权限管理'
                        },
                        component: Rights 
                    },
                    { 
                        path: '/Roles', 
                        name:'Roles',
                        meta:{
                            title: '角色管理'
                        },
                        component: Roles 
                    },
                    { 
                        path: '/categories',
                        name:'categories',
                        meta:{
                            title: '商品参数'
                        }, 
                        component: Cate 
                    },
                    { 
                        path: '/params', 
                        name:'params',
                        meta:{
                            title: '分类参数'
                        },
                        component: Params 
                    },
                    { 
                        path: '/goods', 
                        name:'goods',
                        meta:{
                            title: '商品管理'
                        },
                        component: List 
                    },
                    { 
                        path: '/goods/add',
                        name:'add',
                        meta:{
                            title: '商品添加'
                        },
                        component: Add 
                    },
                    { 
                        path: '/orders', 
                        name:'orders',
                        meta:{
                            title: '订单管理'
                        },
                        component: Order 
                    },
                    { 
                        path: '/reports', 
                        name:'reports',
                        meta:{
                            title: '数据报表'
                        },
                        component: Report 
                    },
                ]
            }
        ]
    })
    // 挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作 next('/login')强制跳转login
    router.beforeEach((to, from, next) => {
    // 访问登录页，放行
    if (to.path === '/login') return next()
        // 获取token
    const tokenStr = window.sessionStorage.getItem('token')
        // 没有token, 强制跳转到登录页
    if (!tokenStr) return next('/login')
    // 渲染路由标题
    document.title = to.meta.title
    next()
})
export default router